package cn.zl.model;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.ObjUtil;
import cn.yunbamboos.Model;
import cn.yunbamboos.mybatis.annotation.TableField;
import cn.yunbamboos.mybatis.annotation.TableName;
import cn.yunbamboos.mybatis.annotation.TablePrimaryKey;
import com.alibaba.fastjson2.JSONObject;

import java.time.LocalDateTime;

@TableName("zl_stream_proxy")
public class StreamProxy implements Model {

    @TablePrimaryKey(value = "proxy_id")
    protected String proxyId;
    @TableField(value = "app")
    protected String app;
    @TableField(value = "stream")
    protected String stream;
    @TableField(value = "url")
    protected String url;
    @TableField(value = "retry_count")
    protected int retryCount;
    @TableField(value = "rtp_type")
    protected int rtpType;
    @TableField(value = "timeout_sec")
    protected int timeoutSec;
    @TableField(value = "enable_audio")
    protected boolean enableAudio;
    @TableField(value = "auto_close")
    protected boolean autoClose;
    @TableField(value = "state")
    private int state;
    @TableField(value = "update_time")
    protected LocalDateTime updateTime;
    @TableField(value = "create_time")
    protected LocalDateTime createTime;

    @Override
    public JSONObject encode() {
        JSONObject json = new JSONObject();
        json.put("proxy_id", proxyId);
        json.put("app", app);
        json.put("stream", stream);
        json.put("url", url);
        json.put("retry_count", retryCount);
        json.put("rtp_type", rtpType);
        json.put("timeout_sec", timeoutSec);
        json.put("enable_audio", enableAudio);
        json.put("auto_close", autoClose);
        json.put("state", state);
        if(ObjUtil.isNotNull(this.updateTime)){
            json.put("update_time", LocalDateTimeUtil.format(updateTime, DatePattern.NORM_DATETIME_PATTERN));
        }
        if(ObjUtil.isNotNull(this.createTime)){
            json.put("create_time", LocalDateTimeUtil.format(createTime, DatePattern.NORM_DATETIME_PATTERN));
        }
        return json;
    }

    @Override
    public void decode(JSONObject json) {
        if (json.containsKey("proxy_id")) this.proxyId = json.getString("proxy_id");
        if (json.containsKey("app")) this.app = json.getString("app");
        if (json.containsKey("stream")) this.stream = json.getString("stream");
        if (json.containsKey("url")) this.url = json.getString("url");
        if (json.containsKey("retry_count")) this.retryCount = json.getInteger("retry_count");
        if (json.containsKey("rtp_type")) this.rtpType = json.getInteger("rtp_type");
        if (json.containsKey("timeout_sec")) this.timeoutSec = json.getInteger("timeout_sec");
        if (json.containsKey("enable_audio")) this.enableAudio = json.getBoolean("enable_audio");
        if (json.containsKey("auto_close")) this.autoClose = json.getBoolean("auto_close");
        if (json.containsKey("state")) this.state = json.getInteger("state");
    }

    public String getProxyId() {
        return proxyId;
    }

    public void setProxyId(String proxyId) {
        this.proxyId = proxyId;
    }

    public String getApp() {
        return app;
    }

    public void setApp(String app) {
        this.app = app;
    }

    public String getStream() {
        return stream;
    }

    public void setStream(String stream) {
        this.stream = stream;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public int getRetryCount() {
        return retryCount;
    }

    public void setRetryCount(int retryCount) {
        this.retryCount = retryCount;
    }

    public int getRtpType() {
        return rtpType;
    }

    public void setRtpType(int rtpType) {
        this.rtpType = rtpType;
    }

    public int getTimeoutSec() {
        return timeoutSec;
    }

    public void setTimeoutSec(int timeoutSec) {
        this.timeoutSec = timeoutSec;
    }

    public boolean isEnableAudio() {
        return enableAudio;
    }

    public void setEnableAudio(boolean enableAudio) {
        this.enableAudio = enableAudio;
    }

    public boolean isAutoClose() {
        return autoClose;
    }

    public void setAutoClose(boolean autoClose) {
        this.autoClose = autoClose;
    }

    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
    }

    public LocalDateTime getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(LocalDateTime updateTime) {
        this.updateTime = updateTime;
    }

    public LocalDateTime getCreateTime() {
        return createTime;
    }

    public void setCreateTime(LocalDateTime createTime) {
        this.createTime = createTime;
    }


}
